{
 "metadata": {
  "name": "",
  "signature": "sha256:7df7b4baff1b5e1c11239ca209fe778170c7b2cd2f8611dc521ecb416f0b685c"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import cv2\n",
      "import numpy as np\n",
      "import pandas as pd\n",
      "import shutil\n",
      "import os"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = pd.read_csv('train.csv')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "label = data.iloc[:, 0]\n",
      "label.to_csv('label.csv', index=None, header=None)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "imgs = data.iloc[:, 1:]\n",
      "print (imgs.shape)\n",
      "len1 = imgs.shape[0]\n",
      "save_path = r\"/media/root/515e7d3a-49ac-40be-ba58-fef9702d123c/work_record/digit_recognizer/digit-recognizer/digit-recognizer/1216/digit\"\n",
      "for i in range(len1):\n",
      "    img = np.array(imgs.iloc[i, :]).reshape(28, 28)\n",
      "    cv2.imwrite(save_path+\"/{}.jpg\".format(i+1), img)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(42000, 784)\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "path = r\"/media/root/515e7d3a-49ac-40be-ba58-fef9702d123c/work_record/digit_recognizer/digit-recognizer/digit-recognizer/1216/digit\"\n",
      "des_path = r\"/media/root/515e7d3a-49ac-40be-ba58-fef9702d123c/work_record/digit_recognizer/digit-recognizer/digit-recognizer/1216/digit/train\"\n",
      "files_list = os.listdir(path)\n",
      "count = 40000\n",
      "index = 0\n",
      "while count:\n",
      "    if os.path.splitext(files_list[index])[1] == \".jpg\":\n",
      "        path_s = os.path.join(path, files_list[index])\n",
      "        path_d = os.path.join(des_path, files_list[index])\n",
      "        shutil.move(path_s, path_d)\n",
      "        count -= 1\n",
      "    index += 1\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "files_list = os.listdir(des_path)\n",
      "len(files_list)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "40000"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "test_data = pd.read_csv('test.csv')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 39
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len1 = test_data.shape[0]\n",
      "save_path = r\"/media/root/515e7d3a-49ac-40be-ba58-fef9702d123c/work_record/digit_recognizer/digit-recognizer/digit-recognizer/1216/digit/test\"\n",
      "\n",
      "for i in range(len1):\n",
      "    img = np.array(test_data.iloc[i, :]).reshape((28, 28))\n",
      "    cv2.imwrite(save_path+\"/{}.jpg\".format(i+1), img)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 44
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}